//index.js
const app = getApp()
const API = require('../../utils/api.js')
Page({
	data: {
		swiperList: [{
		id: 0,
		url: 'https://ossweb-img.qq.com/images/lol/web201310/skin/big84000.jpg'
		}, {
		id: 1,
		url: 'https://ossweb-img.qq.com/images/lol/web201310/skin/big84001.jpg',
		}, {
		id: 2,
		url: 'https://ossweb-img.qq.com/images/lol/web201310/skin/big39000.jpg'
		}, {
		id: 3,
		url: 'https://ossweb-img.qq.com/images/lol/web201310/skin/big10001.jpg'
		}, {
		id: 4,
		url: 'https://ossweb-img.qq.com/images/lol/web201310/skin/big25011.jpg'
		}, {
		id: 5,
		url: 'https://ossweb-img.qq.com/images/lol/web201310/skin/big21016.jpg'
		}, {
		id: 6,
		url: 'https://ossweb-img.qq.com/images/lol/web201310/skin/big99008.jpg'
		}],
		searchResult: null,
		result: false,
    articles: [],
    page: 0,
    pageSize: 5,
    totalDataCount: 0
	},
	onLoad: function () {
    this.getList(0);
    let data = {};
    let that = this;
    that.setData({
      swiperList: []
    })
    data={}

    data.ApiCode = "984ea3c8cf6f4af395b00797b333c73e";
    data.requestDev = "小程序轮播图";
    data.param = "{}";
   
   
    //获取导航图片
    wx.request({
      url: 'https://qxsdcloud.com/data/getMessageRow',
      header: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' },
      data: data,
      method: 'post',
      success: function (e) {
       
        let jsonStr = JSON.parse(e.data.rows)
        app.globalData.lunboList = app.globalData.lunboList.concat(jsonStr);
        if (jsonStr.length > 0) {
          that.setData({
            swiperList: jsonStr
          })
        } 
      },
      fail: function (e) {
        debugger
      }
    });




	},
  getList:function(option){
    // 使用 Mock
    let data = {};
    let that = this;
    
    if (option == 0) {
      app.globalData.indexList = [];
      that.setData({
        articles: null,
        page: 0
      })
    }
    let start = that.data.page * that.data.pageSize;
    let curPage =that.data.page +1;
    data.ApiCode = "b81ad11aa499429dbd518aa4631fba2a";
    data.requestDev = "小程序博客";
    data.param = "{'start':'" + start+"'}";
    const base_url = API.base_url;

  
    wx.request({
      url: 'https://qxsdcloud.com/data/getMessageRow',
      header: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' },
      data: data,
      method: 'post',
      success: function (e) {

        let jsonStr = JSON.parse(e.data.rows)
        app.globalData.indexList = app.globalData.indexList.concat(jsonStr);
       
        let list = app.globalData.indexList.concat(jsonStr);
        console.log(jsonStr.length)
        if (jsonStr.length > 0) {
          that.setData({
            articles: app.globalData.indexList,
            page: curPage
          })
        }else{
          wx.showToast({
            title: '到底啦O(∩_∩)O',
            icon: 'success',
            duration: 2000
          })

        }
       
      

      },
      fail: function (e) {
        debugger
      }
    });

  },
  // 页面上拉触底事件的处理函数 
  onReachBottom: function () {
    this.getList(1)
    wx.showToast({
      title: '加载中...',
      icon: 'loading',
      duration: 1000//持续的时间

    })
   
  },
	onShow:function(){
	},
	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	},
	
	// 自定义方法
	//事件处理函数

	// 文章详情
	showArticle:function (e) {
		console.log(e.currentTarget.dataset.id)
		wx.navigateTo({
			url: '/pages/articleContent/articleContent?id='+e.currentTarget.dataset.id+'&type=index',
		})
	},
  showArticleLunbo: function (e) {
    console.log(e.currentTarget.dataset.id)
    wx.navigateTo({
      url: '/pages/articleContent/articleContent?id=' + e.currentTarget.dataset.id + '&type=lunbo',
    })
  },

	// 搜索
	searchArtile(e) {
		// console.log(e)
		let key = e.detail.value;
		if(key == ' ' || key == null || key.length == 0){
			this.setData({
				result: false
			})
			return
		}else{
			let list = this.data.articles;
			for (let i = 0; i < list.length; i++) {
				let a = key;
				let b = list[i].title;
				this.setData({
					result: true
				})
				if (b.search(a) != -1) {
					list[i].resultShow = true
				} else {
					list[i].resultShow = false
				}
			}
			this.setData({
				searchResult: list
			})
			// console.log(this.data.searchResult)
		}	
	},
  
})
